package com.ruoyi.system.service.impl;

import java.util.List;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.system.vo.DriverCarInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbDriverCarInfoMapper;
import com.ruoyi.system.domain.TbDriverCarInfo;
import com.ruoyi.system.service.ITbDriverCarInfoService;

import static cn.hutool.crypto.digest.DigestUtil.md5Hex;

/**
 * 司机车辆信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-11
 */
@Service
public class TbDriverCarInfoServiceImpl implements ITbDriverCarInfoService 
{
    @Autowired
    private TbDriverCarInfoMapper tbDriverCarInfoMapper;

    /**
     * 查询司机车辆信息
     * 
     * @param id 司机车辆信息主键
     * @return 司机车辆信息
     */
    @Override
    public TbDriverCarInfo selectTbDriverCarInfoById(Long id)
    {
        return tbDriverCarInfoMapper.selectTbDriverCarInfoById(id);
    }

    /**
     * 查询司机车辆信息列表
     * 
     * @param tbDriverCarInfo 司机车辆信息
     * @return 司机车辆信息
     */
    @Override
    public List<TbDriverCarInfo> selectTbDriverCarInfoList(TbDriverCarInfo tbDriverCarInfo)
    {
        return tbDriverCarInfoMapper.selectTbDriverCarInfoList(tbDriverCarInfo);
    }

    /**
     * 新增司机车辆信息
     * 
     * @param tbDriverCarInfo 司机车辆信息
     * @return 结果
     */
    @Override
    public int insertTbDriverCarInfo(TbDriverCarInfo tbDriverCarInfo)
    {
        return tbDriverCarInfoMapper.insertTbDriverCarInfo(tbDriverCarInfo);
    }

    /**
     * 修改司机车辆信息
     * 
     * @param tbDriverCarInfo 司机车辆信息
     * @return 结果
     */
    @Override
    public int updateTbDriverCarInfo(TbDriverCarInfo tbDriverCarInfo)
    {
        return tbDriverCarInfoMapper.updateTbDriverCarInfo(tbDriverCarInfo);
    }

    /**
     * 批量删除司机车辆信息
     * 
     * @param ids 需要删除的司机车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteTbDriverCarInfoByIds(Long[] ids)
    {
        return tbDriverCarInfoMapper.deleteTbDriverCarInfoByIds(ids);
    }

    /**
     * 删除司机车辆信息信息
     * 
     * @param id 司机车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteTbDriverCarInfoById(Long id)
    {
        return tbDriverCarInfoMapper.deleteTbDriverCarInfoById(id);
    }

    @Override
    public DriverCarInfoVO selectTbDriverCarInfoByMerchantNo(String  merchantNo)
    {
        return tbDriverCarInfoMapper.selectTbDriverCarInfoByMerchantNo(merchantNo);
    }

    @Override
    public void updateUserMessagePush(TbDriverCarInfo driverCarInfo) {
        this.tbDriverCarInfoMapper.updateUserMessagePush(driverCarInfo.getId(), driverCarInfo.getCid());
    }

    @Override
    public DriverCarInfoVO selectTInfoByUserId(Long userId) {
        return tbDriverCarInfoMapper.selectTInfoByUserId(userId);
    }

    @Override
    public Integer switchOnlineStatusInterface(Long userId) {

        DriverCarInfoVO driverCarInfoVO = tbDriverCarInfoMapper.selectTInfoByUserId(userId);
        //如果当前司机离线
        Integer i = 2;
        if(driverCarInfoVO.getGoOnline()==2)
        {
            i=1;
            this.tbDriverCarInfoMapper.updateDriverCarInfo(userId, i);
        }else{
            this.tbDriverCarInfoMapper.updateDriverCarInfo(userId, i);
        }
        return i;
    }

    @Override
    public DriverCarInfoVO selectCidInfoByUserId(Long userId) {
        return this.tbDriverCarInfoMapper.selectCidInfoByUserId(userId);
    }

    @Override
    public TbDriverCarInfo selectDriverInfoByMerchantId(String merchantId) {
        return this.tbDriverCarInfoMapper.selectDriverInfoByMerchantId(merchantId);
    }

    @Override
    public void updatePassword(Long userId, String password, String newPassword) {
        TbDriverCarInfo tbDriverCarInfo=this.tbDriverCarInfoMapper.selectTbDriverCarInfoById(userId);
        if(tbDriverCarInfo==null)
        {
            throw  new RuntimeException("用户不存在");
        }
        if(!md5Hex(password + tbDriverCarInfo.getSalt()).equals(tbDriverCarInfo.getPassword()))
        {
            throw  new RuntimeException("原密码错误");
        }
        String salt= RandomUtil.randomString(5);
        String newPassword1=md5Hex(newPassword + salt);
        this.tbDriverCarInfoMapper.updatePassword(userId,newPassword1,salt);
    }

    @Override
    public List<String> resetPasswordsAllDrivers() {
        List<TbDriverCarInfo> tbDriverCarInfos = this.tbDriverCarInfoMapper.selectTbDriverCarInfoList(new TbDriverCarInfo());
        List<String> failedPhones = new java.util.ArrayList<>();
        List<TbDriverCarInfo> updateList = new java.util.ArrayList<>();
        for (TbDriverCarInfo driver : tbDriverCarInfos) {
            try {
                String salt = RandomUtil.randomString(5);
                // 截取后6位（如果不足6位则取全部）
                String lastSixDigits = driver.getPhone().length() > 6 ?
                        driver.getPhone().substring(driver.getPhone().length() - 6) :
                        driver.getPhone();
                // 加盐并MD5加密
                String newPassword1 = md5Hex(lastSixDigits + salt);
                driver.setPassword(newPassword1);
                driver.setSalt(salt);
                updateList.add(driver);
            } catch (Exception e) {
                e.printStackTrace();
                failedPhones.add(driver.getPhone());
            }
        }
        if (!updateList.isEmpty()) {
            try {
                this.tbDriverCarInfoMapper.batchUpdateTbDriverCarInfo(updateList);
            } catch (Exception e) {
                e.printStackTrace();
                for (TbDriverCarInfo driver : updateList) {
                    failedPhones.add(driver.getPhone());
                }
            }
        }
        return failedPhones;
    }
}

